<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES6 Map介绍与API - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES6 Map介绍与API
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/09/26 17:37 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#ES6新特性</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p><code>ES6</code>提供了<code>Map</code>数据结构，它类似于对象，也是键值对的集合，但是<strong>键的范围不限于字符串</strong>，各种类型的值（包括对象）都可以当做键，<code>Map</code>也实现了<code>iterator</code>接口，所以可以使用<code>扩展运算符(...)</code>和<code>for...of</code>进行遍历</p>
<h2><a id="map%E7%9A%84%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Map的属性和方法：</h2>
<ol>
<li><code>size</code><strong>：</strong> 返回<code>Map</code>的元素个数</li>
<li><code>set</code><strong>：</strong> 增加一个新元素，返回当前<code>Map</code></li>
<li><code>get</code><strong>：</strong> 返回键名对象的键值</li>
<li><code>delete</code><strong>：</strong> 删除元素</li>
<li><code>has</code> <strong>：</strong> 检测<code>Map</code>中是否包含某个元素，返回<code>boolean</code>值</li>
<li><code>clean</code><strong>：</strong> 清空集合，返回<code>undefined</code></li>
</ol>
<pre class="line-numbers"><code class="language-js">//声明Map
let m = new Map();

//添加元素
m.set('name','DSR');
m.set('game', function(){
    console.log('打不过就是对面开挂');
});

let key = {
    school:'GDUST'
};
m.set(key,['松山湖','南城']);

//删除
m.delete('name');

//获取
console.log(m.get(key)); //输出 ['松山湖','南城']

//遍历
for(let v of m){
    console.log(v); //输出结果是数组{键,值}
}

//清空
m.clear();
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>